package cn.nawang.ebeim.verification;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.codec.digest.DigestUtils;

public class VerificationMessage {
  private String signature;
  private String userId;
  private String templateId;
  private String sendAddress;
  private String verifyCode;
  private String timestamp;
  private String password;

  public void setVerifyCode(String code) {
    this.verifyCode = code;
  }

  public VerificationMessage(String sendAddress) {
    this(sendAddress, "");
  }


  public VerificationMessage(String sendAddress, String verifyCode) {
    super();

    this.userId = VerificationConfig.getUserId();
    this.password = VerificationConfig.getPassword();
    this.templateId = VerificationConfig.getTemplateId();
    this.timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    this.sendAddress = sendAddress;
    this.verifyCode = verifyCode;
    this.signature =
        DigestUtils.md5Hex(userId + DigestUtils.md5Hex(password) + "nawang" + timestamp);
  }

  public String formatVerifyRequest() {
    try {
      return "user_id=" + URLEncoder.encode(userId, "UTF-8") + "&timestamp="
          + URLEncoder.encode(timestamp, "UTF-8") + "&signature="
          + URLEncoder.encode(signature, "UTF-8") + "&send_address="
          + URLEncoder.encode(sendAddress, "UTF-8") + "&template_id="
          + URLEncoder.encode(templateId, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return "";
  }

  public String formatVerifyCodeRequest() {
    try {
      return "user_id=" + URLEncoder.encode(userId, "UTF-8") + "&timestamp="
          + URLEncoder.encode(timestamp, "UTF-8") + "&signature="
          + URLEncoder.encode(signature, "UTF-8") + "&send_address="
          + URLEncoder.encode(sendAddress, "UTF-8") + "&template_id="
          + URLEncoder.encode(templateId, "UTF-8") + "&verify_code="
          +  URLEncoder.encode(verifyCode, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return "";
    
  }
}
